//LiteXLoader Dev Helper
/// <reference path="c:\Users\stranger\.vscode\extensions\moxicat.lxldevhelper-0.1.4/Library/JS/Api.js" /> 

logger.setConsole(true);
mc.listen("onServerStarted",function(){
    logger.info("[HalfIsland]>>>>>>>>>>>>>>> finished... Summoner加载完毕!");
})

mc.listen("onLeft",function(player){//玩家退出时清除所有召唤物
    if(player&&(player.hasTag("iron")||player.hasTag("snow"))||player.hasTag("ride")){
        player.removeTag("iron");
        player.removeTag("snow");
        player.removeTag("ride");
        player.removeTag("rideCd");
        mc.runcmdEx(`kill @e[tag=${player.name}]`)
    }
})

mc.listen("onUseItemOn",function(player,item,block,side){//召唤物

        if(player&&item&&block&&side&&(item.name==="§3寒霜法杖"||item.name==="§6钢铁意志")){
            if(!player.hasTag("snow")&&!player.hasTag("iron")){//召唤铁傀儡/雪傀儡

                let creatureName="";
                let tickName="";
                let tag="";
                let time=0;

                switch(item.name){
                    case "§3寒霜法杖":{
                        creatureName="minecraft:snow_golem";
                        tickName="§3寒冰意志";
                        tag="snow";
                        time=10000;
                    }break;
                    case "§6钢铁意志":{
                        creatureName="minecraft:iron_golem";
                        tickName="§6钢铁巨兽";
                        tag="iron";
                        time=20000;
                    }break;
                }
                mc.runcmdEx(`title ${player.name} actionbar §4成功召唤${creatureName}`)
                let creature=mc.spawnMob(creatureName,player.pos);//生成实体
                if(creature){

                    creature.addTag(player.name);//把玩家名称作为实体的tag
                    player.addTag(tag)//冷却
                    setTimeout(function(){//技能cd
                        player.removeTag(tag)
                        mc.runcmdEx(`title ${player.name} actionbar §b技能冷却完毕`)
                    },time);
    
                    setTimeout(function(){//生物消失
                        creature.kill();
                        mc.runcmdEx(`title ${player.name} actionbar §b${tickName}已离去`)
                    },time*3);
                    
                }
            }
            else{
                mc.runcmdEx(`title ${player.name} actionbar §b技能冷却中§3>>>>`)
            }

            
        }
});
mc.listen("onUseItemOn",function(player,item,block,side){//召唤羊驼
    if(player&&item.name==="§6召唤坐骑"){
        
        if(!player.hasTag("ride")){
            let en=mc.spawnMob("minecraft:llama",player.pos);
            let armor=en.getArmor();

            if(armor){

                let chest=mc.newItem("chest",1);
                let carpet=mc.newItem("carpet",1);
                let lead=mc.newItem("lead",1);

                player.giveItem(chest);
                player.giveItem(lead);
            
                armor.setItem(1,carpet);
                
                en.refreshItems();//刷新实体物品
                en.addTag(`${player.name}horse`);//单独标识
                en.addTag(player.name);//玩家标识

                player.addTag("ride");
                player.addTag("rideCd");

                mc.runcmdEx(`title ${player.name} actionbar §b坐骑已召唤,已发放箱子和拴绳`);

                setInterval(()=>{
                    player.removeTag("rideCd");
                },10000);

            }
        }else if(!player.hasTag("rideCd")){
            player.removeTag("ride");
            mc.runcmdEx(`kill @e[tag=${player.name}horse]`);
            mc.runcmdEx(`title ${player.name} actionbar §b坐骑已离去`);
        }
        else{
            mc.runcmdEx(`title ${player.name} actionbar §b技能冷却中§3>>>>`);
        }
        
    }
})